package com.hsy.swu.home.grade

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.hsy.swu.hsyutils.MLog
import org.json.JSONObject

@Entity
class GradeModel {
    @ColumnInfo("year")
    var year: String = ""

    @ColumnInfo("semester")
    var semester: String = ""

    @ColumnInfo("name")
    var name: String = ""

    @ColumnInfo("credit")
    var credit: String = ""

    @ColumnInfo("grade")
    var grade: String = ""

    @ColumnInfo("creditGradePoint")
    var creditGradePoint: String = ""

    @ColumnInfo("teacher")
    var teacher: String = ""

    @ColumnInfo("classification")
    var classification: String = ""

    @ColumnInfo("gradeDetail")
    var gradeDetail: String = ""

    @PrimaryKey(autoGenerate = true)
    var id: Int = 0

    override fun toString(): String {
        return "$year\t$semester\t$name\t$credit\t$grade\t$creditGradePoint\t$teacher\t$classification\t$gradeDetail"
    }

    companion object {
        /** 解析基本信息 */
        private fun parseJsonStringToGradeList(
            jsonString: String,
            onParseGradeEnd: (List<GradeModel>) -> Unit
        ) {
            val jsonObj = JSONObject(jsonString)
            val gradeJsonArray = jsonObj.getJSONArray("items")
            val gradeList = mutableListOf<GradeModel>()
            for (i in 0 until gradeJsonArray.length()) {
                val gradeObj = gradeJsonArray.getJSONObject(i)
                gradeList.add(
                    GradeModel().apply {
                        year = gradeObj.getString("xnm")
                        semester = gradeObj.getString("xqm")
                        name = gradeObj.getString("kcmc")
                        credit = gradeObj.getString("xf")
                        grade = gradeObj.getString("cj")
                        creditGradePoint = gradeObj.getString("xfjd")
                        teacher = gradeObj.getString("jsxm")
                        classification = gradeObj.getString("kcxzmc")
                    }
                )
            }
            onParseGradeEnd(gradeList)
        }

        /** 把详细信息合并到基本信息，走完这步才是完整的成绩信息 */
        /** 这里把详细信息解析成 "key-value,key-value" 的存储格式，不然 room不能直接存列表 */
        fun mergeDetailToGradeList(
            simpleJsonString: String, // 成绩基本信息
            detailJsonString: String,
        ): List<GradeModel> {
            if (JSONObject(simpleJsonString).getJSONArray("items").length() == 0
                || JSONObject(detailJsonString).getJSONArray("items").length() == 0
            ) {
                MLog.attention("parseJsonStringToGradeList error -> 为空")
                return emptyList()// 如果 jsonString为空
            }
            var resultList = emptyList<GradeModel>()
            parseJsonStringToGradeList(simpleJsonString) { gradeList ->
                val jsonObject = JSONObject(detailJsonString)
                val gradeDetailJsonArray = jsonObject.getJSONArray("items")
                resultList = gradeList.onEach {
                    var isStart = false // 是否已经遍历到与课程相同的第一个
                    var gradeDetailString = ""
                    for (i in 0 until gradeDetailJsonArray.length()) {
                        val gradeDetailObj = gradeDetailJsonArray.getJSONObject(i)
                        if (it.name == gradeDetailObj.getString("kcmc")) { // 如果课程名称相同
                            isStart = true
                            gradeDetailString +=
                                "${gradeDetailObj.getString("xmblmc")}-${gradeDetailObj.getString("xmcj")},"
                            if (i == gradeDetailJsonArray.length() - 1) { // 最后一个直接给
                                // 截掉最后一个逗号再赋值
                                it.gradeDetail =
                                    gradeDetailString.substring(0, gradeDetailString.length - 1)
                            }
                        } else if (isStart) { // 课程名称不同，但是已经 isStart过了，说明已经遍历完了，则直接 break到下一个 forEach
                            // 截掉最后一个逗号再赋值
                            it.gradeDetail =
                                gradeDetailString.substring(0, gradeDetailString.length - 1)
                            break
                        }
                    }
                }
            }
            return resultList
        }
    }
}